<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<%@ page import="com.wzy.Jdbc封装.pojo.News_detail" %>
<%@ page import="java.util.List" %>
<jsp:useBean id="detail" class="com.wzy.level.detail.detailServiceImpl.detailServiceImpl" />

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>details</title>
</head>
<style type="text/css">
    table,table tr th, table tr td { border: 1px rgba(22, 22, 23, 0.8) solid; }
</style>
<body>
<script type="text/javascript">
    var http_request ="<%=request.getContextPath() %>";
</script>
<%--<jsp:include page="commen.jsp"/>--%>
<div>
    <h1>news_detail表</h1>
    <table>
<%--<%--%>
<%--    List<News_detail> news_details1 = detail.selectAll();--%>
<%--    int count = (int) session.getAttribute("count");--%>
<%--    int total = news_details1.size();--%>
<%--    session.setAttribute("total",total);--%>
<%--    int totalPage =0;--%>
<%--    try {--%>
<%--        totalPage = (int)total/count;--%>
<%--    }catch (Exception e){--%>
<%--        e.printStackTrace();--%>
<%--    }--%>
<%--    session.setAttribute("count1",count);--%>

<%--    session.setAttribute("totalPage",totalPage);--%>
<%--//    session.setAttribute("total_detail",news_details1);--%>


<%--    List<News_detail> news_details = (List<News_detail>) session.getAttribute("news_detail");--%>
<%--    session.setAttribute("news_details",news_details);--%>

<%--%>--%>

<%--            --%>
<%--            --%>

<%--           request.setAttribute("news_details",news_details);--%>

<%--            if(news_details == null){--%>
<%--                news_details =detail.selectAll();--%>
<%--            }--%>
<%--                                             %>--%>
        <c:forEach var="detail" varStatus="id" items="${news_details}" end="${news_details.size()}">

        <tr >
            <%--分开判断就可以隔行不一样的颜色--%>
            <c:if test="${id.count % 2 !=0}">
                <tr bgcolor="#F1A8A8FF">
            </c:if>
            <c:if test="${id.count % 2 ==0}">
                <tr bgcolor="#DFB3F6CC">
            </c:if>
            <td style="width: 50px">${detail.id}</td>
            <td style="width: 100px">${detail.categoryId}</td>
            <td style="width: 100px">${detail.title}</td>
            <td style="width: 150px">${detail.summary}</td>
            <td style="width: 250px">${detail.content}</td>
            <td style="width: 150px">${detail.picPath}
                <div>
                        <%--                    <img style="width: 50px; height: 50px" src="./images/<%=news_details.get(i).getPicPath()%>">--%>
                    <img style="width: 50px; height: 50px" src="${detail.picPath}">
                </div>
            </td>
            <td>${detail.author}</td>
            <td>
                <fmt:formatDate value="${detail.date}" pattern="yyyy-MM-dd"></fmt:formatDate></td>
            <td>
                <fmt:formatDate value="${detail.date1}" pattern="yyyy-MM-dd"></fmt:formatDate></td>
<%--            <td><a href="detail_delete.jsp?id=${detail.id}">删除</a></td>--%>
<%--            <td><a href="<%=path%>/detail_delete?id=${detail.id}">删除</a></td>--%>
<%--            <td><a href="/detail_delete?id=${detail.id}">删除</a></td>--%>
            <td onclick="del(${detail.id})">删除</td>
<%--            <td><a href="/insert_updateServlet?state=0&id=${detail.id}">修改</a></td>--%>
            <td ><button onclick="modify(${detail.id})">修改</button> </td>

<%--            <td><a href="/insert_updateServlet?state=1&id=${detail.id}">新增</a></td>--%>
            <td><button onclick="insert(${detail.id})" >新增</button></td>
        </tr>
        </c:forEach>
        <tr>
<%--            <td><a href="/pageManage?index=1&count=${sessionScope.count}&cate=1">首页</a></td>--%>
            <td onclick="shou(${sessionScope.count})">首页</td>
            <td style="width: 100px" onclick="pre(${(sessionScope.index)<0? 0:(index)},${sessionScope.count})">&lt;&lt;上一页</td>
            <td onclick="next(${(sessionScope.index+2)},${sessionScope.count})">下一页&gt;&gt;</td>
            <td onclick="wei(${totalPage+1},${sessionScope.count})">尾页</td>
            <td>第${sessionScope.index+1}页</td>
            <td>共${(total % sessionScope.count == 0) ? totalPage : totalPage+1}页</td>
            <td>共${total}条记录</td>
            <td>
<%--                <form action="/pageManage" method="get" style="margin: 0 auto">--%>
                <form action="#"  style="margin: 0 auto">
                    一页查<input type="text"  id="count" style="width: 50px" >条数据&nbsp;
                    <input type="text" name="cate"  style="width: 50px" value="1" hidden >
                    跳转至
                    <input type="text"  id="index" style="width: 50px"  >
                    页
<%--                    <input type="submit" value="GO">--%>
                </form>
                <button onclick="go()">GO</button>
            </td>


        </tr>


    </table>
    <h1 id="suc"></h1>
<%--<img src="file:/D:/web_project/src/main/webapp/images/女孩.jpg">--%>
</div>
<%--<iframe style="width: 1000px" src="index.jsp">--%>
<%--</iframe>--%>

<script type="text/javascript" src="../js/jquery-3.2.1.min.js"></script>
<script type="text/javascript">
    function del(id){
        //创建对象
        var obj = new XMLHttpRequest();
        //建立连接 连接到服务器
        var url = "/detail_delete?id="+id;
        obj.open("get",url,true);
        //发出请求
        obj.send();
        //服务端响应
        obj.onreadystatechange = function (){
            if(obj.readyState == 4 && obj.status == 200){
                // alert("请求成功!"+obj.responseText );
                var text = obj.responseText;
                if(text == 1){
                    alert("删除成功")
                    //刷新页面
                    // window.location.href="/pageManage?cate=1";
                    $.ajax({
                        url:"/pageManage",
                        data:{
                            cate:1
                        },
                        method:"get",
                        dataType:"text",
                        success:function (data){
                            // alert(1)
                            if(data == 1){

                                alert("删除后再次进入新闻详细页面！")
                                window.location.href="/detail";//处理detail表页面的数据
                            }
                        }
                    })
                }else {
                    alert("删除失败")
                }
            }
        }
    }

   function modify(detailId){//改
            // alert(1)
            $.ajax({
                url: "/insert_updateServlet",
                method: "get",
                dataType:"text",
                data: {
                    "id":detailId,
                    "state":0
                },
                success:function (data){
                    if(data==1){
                        // alert(2)
                        // $.ajax({
                        //     url: http_request+"/system/detail_form.jsp",
                        //     method: "get",
                        //     dataType:"text",
                        //     data: {},
                        //     success:function (){
                                    alert("修改中间站！")
                        window.location.href=http_request+"/system/detail_form.jsp";
                            }
                        // })
                    // }
                },
                error:function (){

                }
            })
        }


    function insert(detailId){//增

        $.ajax({
            url: "/insert_updateServlet",
            method: "get",
            dataType:"text",
            data: {
                "id":detailId,
                "state":1
            },
            success:function (data){
                if(data==1){
                    // alert(2)
                    // $.ajax({
                    //     url: http_request+"/system/detail_form.jsp",
                    //     method: "get",
                    //     dataType:"text",
                    //     data: {},
                    //     success:function (){
                    alert("新增中间站！")
                    window.location.href=http_request+"/system/detail_form.jsp";
                }
                // })
                // }
            },
            error:function (){

            }
        })
    }

    function go(){
        var count = $("#count").val();
        console.log(count)
        var index = $("#index").val();
        $.ajax({
            url:"/pageManage",
            data:{
                index:index,
                count:count,
                cate:1,
            },
            method:"get",
            type:"text",
            success:function (date){
                if(date==1){
                    alert("更新新的分页查询")
                    window.location.href="/detail";//处理detail表页面的数据
                }
            }
        })
    }

    function shou(count){
        $.ajax({
            url:"/pageManage",
            data:{
                index:1,
                count:count,
                cate:1,
            },
            method:"get",
            type:"text",
            success:function (date){
                if(date==1){
                    alert("首页")
                    window.location.href="/detail";//处理detail表页面的数据
                }
            }
        })
    }
    function pre(index,count){
        $.ajax({
            url:"/pageManage",
            data:{
                index:index,
                count:count,
                cate:1,
            },
            method:"get",
            type:"text",
            success:function (date){
                if(date==1){
                    alert("上一页")
                    window.location.href="/detail";//处理detail表页面的数据
                }
            }
        })
    }
    function next(index,count){
        $.ajax({
            url:"/pageManage",
            data:{
                index:index,
                count:count,
                cate:1,
            },
            method:"get",
            type:"text",
            success:function (date){
                if(date==1){
                    alert("下一页")
                    window.location.href="/detail";//处理detail表页面的数据
                }
            }
        })
    }
    function wei(index,count){
        $.ajax({
            url:"/pageManage",
            data:{
                index:index,
                count:count,
                cate:1,
            },
            method:"get",
            type:"text",
            success:function (date){
                if(date==1){
                    alert("尾页")
                    window.location.href="/detail";//处理detail表页面的数据
                }
            }
        })
    }

</script>
</body>
</html>
